<?php
include_once(LIB."Database.class.php");
class Model {
	// 定义数据库
	var $db = null;
	// 模型名称
	protected $name = '';
	//定义单例模式的数据库
	private static $_instance_db;
	//定义redis
	//var $redis;
	//定义单例模式的redis
	//private static $_instance_redis;

	//构造函数
	function Model($name = '') {
		global $db_config;
		if(!self::$_instance_db){
			self::$_instance_db = new Database($db_config);
		}
		$this->db = self::$_instance_db;
		// 获取模型名称
		if(!empty($name)) {
			$this->name = $name;
		}elseif(empty($this->name)){
			$this->name = $this->getModelName();
		}
	}


	//初始化redis，连接redis，实例化redis对象
	/*function init_redis(){
		if(!self::$_instance_redis){
		$redis = redis();
		self::$_instance_redis = $redis;
		}
		$this->redis = self::$_instance_redis;
		}

		function init_database($db_config){
		if(!self::$_instance_db){
		self::$_instance_db = new Database();
		self::$_instance_db -> connect($db_config['host'],$db_config['user'],$db_config['pass'],$db_config['database']);
		}
		$this->db = self::$_instance_db;
		}*/

	//得到当前的数据对象名称
	public function getModelName() {
		if(empty($this->name))
		$this->name = substr(get_class($this),0,-5);
		return $this->name;
	}

	//只读操作
	function query($sql){
		return $this->db->query($sql);
	}
	//可写操作
	function execute($sql) {
		return $this->db->execute($sql);
	}
	
	//关闭数据库
	function close(){
		return $this->db->close();
	}
	
	function __destruct(){
		
	}
}
?>